package com.gopay.scheduler.job;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.constants.trans.TxnStaCode;
import com.gopay.common.cps.dao.order.CpsGeneralMainOrderQueryDAO;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.order.MainOrderField;
import com.gopay.remote.order.MainOrderUpdateRemoteService;
import com.gopay.scheduler.GopayBaseJob;
import com.gopay.scheduler.common.OrderLifecycleUtil;


/**
 * 跨境人民币的01720,01723,01724交易将进行中(30000,30101)的订单置为失效(20002)
 * 01723,01724交易的支付成功，主订单状态是30402
 * @author qld
 * @author qld 2017-11-3 modify  加上了01740交易的支付方式是70-外卡支付 算B2C  24小时置失效
 */
public class CBRMBPayOrdersTimeoutCleanJob extends GopayBaseJob {
    public static final String TRANSTYPE = "01720";
    private Logger logger = LoggerFactory.getLogger(CBRMBPayOrdersTimeoutCleanJob.class);
    private CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderDAO;
    private MainOrderUpdateRemoteService cpsGeneralMainOrderOperClient;

    private static final int QUERYCOUNT = 5000;

    // private static final int REMAINDER = QUERYCOUNT -1;
    @Resource(name = "cpsGeneralMainOrderOperClient")
    public void setCpsGeneralMainOrderOperClient(MainOrderUpdateRemoteService cpsGeneralMainOrderOperClient) {
        this.cpsGeneralMainOrderOperClient = cpsGeneralMainOrderOperClient;
    }

    @Resource(name = "cpsGeneralMainOrderDAO")
    public void setCpsGeneralMainOrderDAO(CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderDAO) {
        this.cpsGeneralMainOrderDAO = cpsGeneralMainOrderDAO;
    }

    @Override
    public void doJobInternal() {
        
        Map param = new HashMap();
        String B2bName = TRANSTYPE + OrderLifecycleUtil.B2B;
        String B2cName = TRANSTYPE + OrderLifecycleUtil.B2C;
        String b2c = OrderLifecycleUtil.getLifecycle(B2cName);
        String b2b = OrderLifecycleUtil.getLifecycle(B2bName);
        param.put("b2b", b2b);
        param.put("b2c", b2c);
        long startMillions = System.currentTimeMillis();
        logger.info("查询跨境人民币超时订单执行开始 ");
        List<Object> orderList = cpsGeneralMainOrderDAO.findTimeoutOrdersInCBPay(param, 1, QUERYCOUNT);
        logger.info("查询跨境人民币超时订单个数:"+orderList.size());
        updateStatus(orderList);

    }

    private void updateStatus(List<Object> orderList) {
        if (!(orderList.isEmpty())) {
            String txnStaCode = String.valueOf(TxnStaCode.TXN_STA_CD_20002.value);
            //logger.info("pay timeout orders Id:" + orderList.toString());
            // logger.info("found" + orderList.size() + "other orders  records, overtime timeout order clean-up work now!");

            Map<MainOrderField, String> map = new HashMap<MainOrderField, String>();

            map.put(MainOrderField.TXN_STA_CD, txnStaCode);
            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
            map.put(MainOrderField.TRAN_FINISH_TIME, sf.format(new Date()));
            for (Object obj : orderList) {

                String gopayOrderId = String.valueOf(obj);
                try {
                    cpsGeneralMainOrderOperClient.updateMany(gopayOrderId, map);

                } catch (GopayException e) {
                    logger.error("update order fail,orderId:" + gopayOrderId.toString(), e);
                } catch (Exception e) {
                    logger.error("update order fail,orderId:" + gopayOrderId, e);
                }

            }

        }
        logger.info(orderList.size()+"个跨境人民币订单状态置为失效成功");
    }
}
